package com.cg.controller;

import com.cg.dto.OrgPageDto;
import com.cg.dto.Result;
import com.cg.dto.ResultEnum;
import com.cg.dto.ResultUtils;
import com.cg.entity.Organize;
import com.cg.service.OrganizeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Organize)表控制层
 *
 * @author chengong
 * @since 2023-12-18 18:10:30
 */
@RestController
@RequestMapping("organize")
public class OrganizeController {
    /**
     * 服务对象
     */
    @Resource
    private OrganizeService organizeService;

    /**
     * 验证Code是否存在
     */
    @GetMapping("/queryCountByOrgCode")
    public Result queryCountByOrgCode(@RequestParam("orgName") String orgName){
        Integer count = organizeService.queryCountByOrgCode(orgName);
        if(count > 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_ORGANIZE_CODE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 新增组织编码
     */
    @PostMapping("/createOrganize")
    public Result createOrganize(@RequestBody Organize organize){
        Integer count = organizeService.createOrganize(organize);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_ORGANIZE_CODE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 查询组织全部信息-分页并按照名称模糊查询
     */
    @PostMapping("/organizeList")
    public Result<Map<String,Object>> organizeList(@RequestBody OrgPageDto orgPageDto){
        PageInfo<Organize> pageInfo = organizeService.organizeList(orgPageDto);
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        return ResultUtils.returnDataSuccess(map);
    }


    /**
     * 修改组织信息
     */
    @PostMapping("/setOrganize")
    public Result setOrganize(@RequestBody Organize organize){
        Integer count = organizeService.setOrganize(organize);
        if(count <= 0){
            return ResultUtils.returnFail("修改失败");
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 删除组织信息
     */
    @PostMapping("/removeOrganize")
    public Result removeOrganize(@RequestParam("id") Integer id){
        Integer count = organizeService.removeOrganize(id);
        if(count <= 0){
            return ResultUtils.returnFail("删除失败");
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 查询组织详情
     */
    @PostMapping("/queryOrganizeByOrganizeId")
    public Result<Organize> queryOrganizeByOrganizeId(@RequestParam("organizeId") Integer organizeId){
        Organize organize = organizeService.queryOrganizeByOrganizeId(organizeId);
        return ResultUtils.returnDataSuccess(organize);
    }

}

